package ruide_info_mvc.base.core.bean_core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ruide_info_mvc.base.contoller.BaseContoller;
import ruide_info_mvc.base.core.aop.ICustomAop;
import ruide_info_mvc.base.core.exception.customException.CommonException;
import ruide_info_mvc.base.core.ioc.CustomIoc;
import ruide_info_mvc.base.model.request.BaseRequestBean;
import ruide_info_mvc.base.model.response.BaseResponseBean;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @ClassName BeanInvokeCore
 * @Author wangBo
 * @Date 2019/7/21 0021 上午 10:10
 * @Version 1.0
 */
@Service(value = "beanInvokeCore")
public class BeanInvokeCore extends AbstractBeanInvokeCore {

    private final Logger logger = LoggerFactory.getLogger(BaseContoller.class);

    @Override
    protected BaseResponseBean invoke_(Object obj ,Method method, BaseRequestBean baseRequestBean, MultipartFile[] files) throws Exception {
        BaseResponseBean baseResponseBean = new BaseResponseBean();
        try {
            Class [] paramsClassArray = method.getParameterTypes();
            if (paramsClassArray.length > 0) {
                baseRequestBean = baseRequestBean.toBean(paramsClassArray[0]);
                if (paramsClassArray.length == 1) {
                    if (paramsClassArray[0].isInstance(baseRequestBean)) {
                        baseResponseBean = (BaseResponseBean)method.invoke(obj , baseRequestBean);
                    }
                } else if  (paramsClassArray.length == 2) {
                    Object [] objParamsArray = new Object[2];
                    objParamsArray[0] = baseRequestBean;
                    objParamsArray[1] = paramsClassArray[0].isInstance(files) ? files : paramsClassArray[1].isInstance(files) ? files : null;
                    baseResponseBean = (BaseResponseBean)method.invoke(obj , objParamsArray);
                }
            } else {
                baseResponseBean = (BaseResponseBean)method.invoke(obj , null);
            }
        } catch (IOException e) {
            logger.error("" , e);
            throw e;
        } catch (IllegalAccessException e) {
            logger.error("" , e);
            throw new CommonException("参数异常!" , false);
        } catch (IllegalArgumentException e) {
            logger.error("" , e);
            throw new CommonException("参数异常!" , false);
        } catch (InvocationTargetException e) {
            logger.error("" , e);
            throw new CommonException(((CommonException)e.getCause()).getErrorMsg() , false);
        }
        return baseResponseBean;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            customAop = CustomIoc.getApplicationContext().getBean(ICustomAop.class);
        } catch (BeansException e) {
            logger.error("" , e);
            throw new CommonException("初始化异常!~" , false);
        }
    }
}
